<?php

// @formatter:off
/**
 * @file WCRulesHolder.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * This file contains definition for 'WCRulesHolder' class.
 *
 * $Id: WCRulesHolder.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/includes/holders/WCRulesHolder.php $
 */
// @formatter:on

/**
 * @class WCRulesHolder
 * This singleton class is the one in charge of managing all rules. It's purpose
 * is to concentrate all rules fetches in one place avoiding unneeded memory
 * allocation.
 *
 * By default, this class only loads basic information on creation, but no heavy
 * lists. Lists are loaded only where they are needed.
 */
class WCRulesHolder extends WCHolderGroup implements WCSearchIndexableHolder {
	/**
	 * @var string Class name of the objects to be held.
	 */
	protected $_PropertiesItemClass = "WCRule";
	/**
	 * @var string This is the prefix of every column on the table
	 * '%rules'.
	 */
	protected $_PropertiesPrefix = "rul_";
	/**
	 * @var string This is the name of the table represented by WCRule
	 * objects. In this case, '%rules'.
	 */
	protected $_PropertiesTable = "rules";
	//
	// Public methods.
	public function notIndexedId() {
		$out = array();
		//
		// Creating the statement to select non indexed tags.
		// @{
		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__."[{$this->_PropertiesTable}]";
		if($this->_db->has($stmtId)) {
			$stmt = $this->_db->get($stmtId);
		} else {
			$query = "select  {$this->_PropertiesPrefix}id as id\n";
			$query .= "from    {$this->_dbprefix}{$this->_PropertiesTable}\n";
			$query .= "where   {$this->_PropertiesPrefix}indexed = 'N'\n";

			$stmt = $this->_db->prepare($stmtId, $query);
		}
		// @}

		if($stmt->execute() && $stmt->rowCount() > 0) {
			foreach($stmt->fetchAll() as $row) {
				$out[] = $row["id"];
			}
		}

		return $out;
	}
	public function searchItemsType() {
		return "RL";
	}
	//
	// Public class methods.
	/**
	 * This class-method tries to create an object to hold and manage an
	 * action. Such new object depends on it's type and the hooked class for
	 * that type.
	 *
	 * @see $wcRuleHooks
	 *
	 * @param integer $type Code to identificate the right hook.
	 * @param integer $id Action identificator code.
	 * @param WCGroup $group Group assosiated with the requested action.
	 * @return WCRuleAction return the action requested built with its own
	 * class.
	 */
	public static function ActionFactory($actionType, $ruleId, $actionId, WCGroup &$group) {
		//
		// Setting a default value to be returned.
		$out = false;
		//
		// Loading global pointers.
		global $wcRuleHooks;
		//
		// Checking if there's a class defined for the action type
		// requested.
		if(isset($wcRuleHooks[$actionType]) && isset($wcRuleHooks[$actionType]["class"]) && class_exists($wcRuleHooks[$actionType]["class"])) {
			//
			// Creating a class shortcut from hooks.
			$c = $wcRuleHooks[$actionType]["class"];
			//
			// Creating an object that will manage the specified
			// action type.
			$out = new $c($ruleId, $actionId, $group);
		} elseif(!isset($wcRuleHooks[$actionType]["class"])) {
			//
			// When an action type is requested and it has a hook, but
			// but such hook has no class specification, it triggers
			// an error.
			trigger_error(__CLASS__."::".__FUNCTION__."(): Action Type '{$actionType}' unknown", E_USER_ERROR);
		} elseif(!class_exists($wcRuleHooks[$actionType]["class"])) {
			//
			// When an action type is requested, it has a hook and a
			// class specification, but such class doesn't exist, it
			// triggers an error.
			trigger_error(__CLASS__."::".__FUNCTION__."(): Action Type '{$actionType}' unknown", E_USER_ERROR);
		} else {
			//
			// When an action type is requested but there's no hook
			// for it, it triggers an error.
			trigger_error(__CLASS__."::".__FUNCTION__."(): Action Type '{$actionType}' unknown", E_USER_ERROR);
		}
		//
		// Returning the action object requested.
		return $out;
	}
}
